package jiaerc.moreingredients.handler;

import net.minecraft.entity.LivingEntity;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.util.DamageSource;
import net.minecraft.util.Util;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.event.entity.living.PotionEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import jiaerc.moreingredients.MoreIngredientsEffects;

@Mod.EventBusSubscriber
public class PotionEventHandler {

    @SubscribeEvent//检测事件
    public static void onPotionAdded(PotionEvent.PotionAddedEvent event) {
        LivingEntity entity = event.getEntityLiving();
        EffectInstance newEffect = event.getPotionEffect();

        // 仅处理辛辣效果
        if (newEffect.getEffect() == MoreIngredientsEffects.SPICY_EFFECT.get()){
            // 立即造成伤害（伤害值 = 基础1点 + 效果等级）
            entity.hurt(DamageSource.MAGIC, 1.0F + newEffect.getAmplifier());
            // 发送警告消息
            entity.sendMessage(
                    new TranslationTextComponent("message.moreingredients.spicy_warning"),
                    Util.NIL_UUID
            );
        }
        //如果获得辛辣效果，就去除中毒效果
        if (newEffect.getEffect() == MoreIngredientsEffects.SPICY_EFFECT.get()) {
            if (entity.hasEffect(Effects.POISON)) {
                entity.removeEffect(Effects.POISON);
            }
        }
    }
}